from PyQt5.QtWidgets import (QDialog, QLabel, QLineEdit, QPushButton,
                             QVBoxLayout, QHBoxLayout, QFrame, QWidget)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont, QPixmap


class LoginWindow(QDialog):
    def __init__(self, db):
        super().__init__()
        self.db = db
        self.setWindowTitle("银行系统登录")
        self.setFixedSize(1000, 600)
        self.setWindowFlags(Qt.FramelessWindowHint)  # 去除窗口边框

        # 创建主容器（外围白边效果）
        main_container = QFrame(self)
        main_container.setGeometry(5, 5, 990, 590)
        main_container.setStyleSheet(
            "QFrame {"
            "   background-color: #dbe2ef;"
            "   border-radius: 0px;"
            "}"
        )

        # 主布局
        main_layout = QHBoxLayout(main_container)
        main_layout.setContentsMargins(0, 0, 0, 0)
        main_layout.setSpacing(0)

        # 创建左侧框架
        left_frame = QFrame()
        left_frame.setFixedWidth(400)
        left_frame.setStyleSheet(
            "QFrame {"
            "   background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 #2193b0, stop:1 #6dd5ed);"
            "   border-radius: 0px;"
            "}"
        )

        left_layout = QVBoxLayout(left_frame)
        left_layout.setContentsMargins(30, 40, 30, 30)
        left_layout.setSpacing(0)

        self.left_title = QLabel("管理员登录")
        self.left_title.setFont(QFont("Microsoft YaHei", 30, QFont.Bold))
        self.left_title.setStyleSheet(
            "color: white; " 
            "padding: 0; "
            "margin: 0; "
            "letter-spacing: 2px;"
        )
        self.left_title.setAlignment(Qt.AlignCenter)
        left_layout.addStretch(1)
        left_layout.addWidget(self.left_title)
        left_layout.addStretch(1)

        # 切换按钮
        self.switch_button = QPushButton("去客户端")
        self.switch_button.setFixedSize(120, 40)
        self.switch_button.setFont(QFont("Microsoft YaHei", 10))
        self.switch_button.setStyleSheet(
            "background-color: transparent;"
            "color: white;"
            "border: 1px solid white;"
            "border-radius: 20px;"
            "font-weight: bold;"
            "font-size: 14px;"
        )
        self.switch_button.clicked.connect(self.toggle_login_mode)

        # 按钮容器
        button_container = QWidget()
        button_layout = QVBoxLayout(button_container)
        button_layout.setContentsMargins(0, 0, 0, 0)
        button_layout.addStretch(1)
        button_layout.addWidget(self.switch_button, 0, Qt.AlignRight)

        left_layout.addWidget(button_container)
        main_layout.addWidget(left_frame)

        # 右侧面板 - 表单区域
        right_frame = QFrame()
        right_frame.setFixedWidth(600)
        right_frame.setStyleSheet(
            "background-color: white;"
            "border-radius: 0px;"
        )

        right_layout = QVBoxLayout(right_frame)
        right_layout.setContentsMargins(60, 60, 60, 40)
        right_layout.setSpacing(20)

        # 添加 logo
        logo_label = QLabel()
        pixmap = QPixmap(r"D:\0000\pythonProject1\bank_manage_system\logo.png")  # 确保路径正确
        if not pixmap.isNull():
            # 调整图片大小
            pixmap = pixmap.scaled(400, 80, Qt.AspectRatioMode.KeepAspectRatio)
            logo_label.setPixmap(pixmap)
            logo_label.setAlignment(Qt.AlignCenter)
        else:
            print("图片加载失败，请检查路径是否正确")
        right_layout.addWidget(logo_label)

        # 登录类型
        self.login_type = QLabel("管理员登录")
        self.login_type.setFont(QFont("Microsoft YaHei", 22))
        self.login_type.setStyleSheet("color: #666;")
        self.login_type.setAlignment(Qt.AlignCenter)
        right_layout.addWidget(self.login_type)
        right_layout.addSpacing(10)

        # 表单区域
        form_layout = QVBoxLayout()
        form_layout.setContentsMargins(0, 20, 0, 0)
        form_layout.setSpacing(25)

        # 账号区域
        self.account_label = QLabel("账号")
        self.account_label.setFont(QFont("Microsoft YaHei", 15))
        self.account_label.setStyleSheet("color: #333;")
        form_layout.addWidget(self.account_label)

        self.account_input = QLineEdit()
        self.account_input.setFont(QFont("Microsoft YaHei", 14))
        self.account_input.setFixedHeight(50)
        self.account_input.setStyleSheet(
            "border: 2px solid #e0e0e0;"
            "border-radius: 6px;"
            "padding: 0 15px;"
        )
        self.account_input.setText("adminlmx")
        self.account_input.setReadOnly(True)
        form_layout.addWidget(self.account_input)

        # 密码区域
        self.password_label = QLabel("密码")
        self.password_label.setFont(QFont("Microsoft YaHei", 15))
        self.password_label.setStyleSheet("color: #333;")
        form_layout.addWidget(self.password_label)

        self.password_input = QLineEdit()
        self.password_input.setEchoMode(QLineEdit.Password)
        self.password_input.setPlaceholderText("请输入管理员密码")
        self.password_input.setFont(QFont("Microsoft YaHei", 14))
        self.password_input.setFixedHeight(50)
        self.password_input.setStyleSheet(
            "border: 2px solid #e0e0e0;"
            "border-radius: 6px;"
            "padding: 0 15px;"
        )
        form_layout.addWidget(self.password_input)

        # 登录按钮
        login_btn = QPushButton("登录")
        login_btn.setFont(QFont("Microsoft YaHei", 18, QFont.Bold))
        login_btn.setFixedHeight(50)
        login_btn.setStyleSheet(
            "background-color: #00b8a9;"
            "color: white;"
            "border-radius: 6px;"
        )
        login_btn.clicked.connect(self.authenticate)
        form_layout.addWidget(login_btn)

        right_layout.addLayout(form_layout)
        right_layout.addStretch(1)

        # 版权信息
        copyright_label = QLabel("©2025 银行系统 版权所有")
        copyright_label.setFont(QFont("Microsoft YaHei", 10))
        copyright_label.setStyleSheet("color: #999;")
        copyright_label.setAlignment(Qt.AlignCenter)
        right_layout.addWidget(copyright_label)

        main_layout.addWidget(right_frame)

        # 错误提示
        self.error_label = QLabel()
        self.error_label.setFont(QFont("Microsoft YaHei", 12))
        self.error_label.setStyleSheet("color: #e53935;")
        self.error_label.setVisible(False)
        form_layout.addWidget(self.error_label)

        # 设置当前登录模式
        self.is_admin_mode = True

        # 右上角关闭按钮
        self.close_btn = QPushButton("×", self)
        self.close_btn.setFixedSize(40, 40)
        self.close_btn.setGeometry(960, 5, 40, 40)
        self.close_btn.setFont(QFont("Microsoft YaHei", 24))
        self.close_btn.setStyleSheet(
            "background-color: rgba(245, 245, 245, 0.7);"
            "border: none;"
            "border-radius: 20px;"
            "color: #999;"
            "font-weight: bold;"
        )
        self.close_btn.clicked.connect(self.close)

    def toggle_login_mode(self):
        try:
            self.is_admin_mode = not self.is_admin_mode

            if self.is_admin_mode:
                self.left_title.setText("管理员登录")
                self.login_type.setText("管理员登录")
                self.switch_button.setText("去客户端")
                self.account_label.setText("账号")
                self.account_input.setText("adminlmx")
                self.account_input.setReadOnly(True)
                self.password_input.setPlaceholderText("请输入管理员密码")
            else:
                self.left_title.setText("客户登录")
                self.login_type.setText("客户登录")
                self.switch_button.setText("去管理端")
                self.account_label.setText("银行卡号")
                self.account_input.setText("")
                self.account_input.setReadOnly(False)
                self.password_input.setPlaceholderText("请输入银行卡密码")

            self.password_input.clear()
            self.error_label.setVisible(False)

        except Exception as e:
            self.is_admin_mode = True
            self.left_title.setText("管理员登录")
            self.login_type.setText("管理员登录")
            self.switch_button.setText("去客户端")

    def authenticate(self):
        try:
            username = self.account_input.text().strip()
            password = self.password_input.text().strip()

            if self.is_admin_mode:
                if password == "123":  # 实际系统应使用加密验证
                    self.accept()
                    return "管理员", username
                else:
                    self.error_label.setText("管理员密码错误")
                    self.error_label.setVisible(True)
                    return None
            else:
                if not (username and password):
                    self.error_label.setText("请输入银行卡号和密码")
                    self.error_label.setVisible(True)
                    return None

                try:
                    query = "SELECT * FROM cardinfo WHERE cardID = %s AND pass = %s"
                    result = self.db.execute_query(query, (username, password))
                    if result:
                        if result[0]["IsReportLoss"] == 1:
                            self.error_label.setText("该银行卡已挂失，请前往柜台处理")
                            self.error_label.setVisible(True)
                            return None

                        self.accept()
                        return "客户", username
                    else:
                        self.error_label.setText("银行卡号或密码错误")
                        self.error_label.setVisible(True)
                        return None
                except Exception as e:
                    print(f"数据库查询错误: {str(e)}")
                    self.error_label.setText("系统错误，请联系管理员")
                    self.error_label.setVisible(True)
                    return None

        except Exception as e:
            print(f"登录过程中出现错误: {str(e)}")
            self.error_label.setText("系统错误，请联系管理员")
            self.error_label.setVisible(True)
            return None